// shop/buy/buy.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    inputValue:'',
    dingdan:'',
    addr:'',
    city:false,
    region: ['', '', ''],
    customItem: '全部',
    id: wx.getStorageSync('userid')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      dingdan:options.id,
      id: wx.getStorageSync('userid')
    })
    wx.request({
      url: `https://www.prebeauty.work:3001/api/dingdan?id=${options.id}`,
      header: {
        'content-type': 'application/json'
      },
      success:async (res)=>{
        console.log(res.data)
        this.setData({
          list:res.data[0]
        })
        console.log(res.data[0])
        if(res.data[0].type == '2'){
          this.setData({
            addr: '河北师大救助站',
            region: ['河北省', '石家庄市', '裕华区']
          })
        }
        console.log(this.data)
      }
    })
  },

  onReady:function(){
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!wx.getStorageSync('token')){
      wx.showModal({
        title: '用户未登录',
        content: '请先进行授权登录',
        success(res) {
          if (res.confirm) {
            console.log('确定')
            wx.switchTab({
              url: '/pages/home/home'
           })
          } else if (res.cancel) {
            console.log('取消')
            wx.switchTab({
              url: '/pages/shop/shop'
           })
          }
        }
      })
    }
  },

  contentSaveTime:0,
  handleInputChange:function(e) {
    this.setData({
      inputValue:e.detail.value
    })
    let tm = Date.now();
    if((this.contentSaveTime+500)>tm){
      return;
    }
    this.contentSaveTime=tm;
    console.log('获取输入',e.detail.value);
    //实时缓存表单输入
    wx.setStorageSync('home-value', encodeURIComponent(e.detail.content));
  },
  addrSaveTime:0,
  handleAddrChange:function(e) {
    if(this.data.region[0]==''||this.data.region[0]=='全部'||this.data.region[1]==''||this.data.region[1]=='全部'||this.data.region[2]==''||this.data.region[2]=='全部'){
      wx.showToast({
        title: '先选省市县哦',
        icon: 'none',
        duration: 2000
      })
    }
    this.setData({
      addr:e.detail.value
    })
    let tm = Date.now();
    if((this.addrSaveTime+500)>tm){
      return;
    }
    this.addrSaveTime=tm;
    console.log('获取输入',e.detail.value);
    //实时缓存表单输入
    wx.setStorageSync('home-value', encodeURIComponent(e.detail.content));
  },

  onpay: function(e){
    // console.log(1)
    wx.request({
      url: `https://www.prebeauty.work:3001/api/dingdan/${this.data.dingdan}`,
      header: {
        'content-type': 'application/json'
      },
      data:{
        address: `${this.data.region[0]}${this.data.region[1]}${this.data.region[2]}${this.data.addr}`,
        pay:'已支付',
        beizhu:this.data.inputValue
      },
      method: 'PUT',
      success:async (res)=>{
        console.log(res.data)
        wx.showModal({
          title:'支付成功',
          content:'再去看看吧！！',
          success(res) {
            if (res.confirm) {
              console.log('确定')
              wx.switchTab({
                url: '/pages/shop/shop'
             })
            } else if (res.cancel) {
              console.log('取消')
              wx.switchTab({
                url: '/pages/index/index'
             })
            }
          }         
        })
      }
    })
    var util=require('../../pages/utils/util.js')
    var time=util.formatTime(new Date())
    if(this.data.list.type == '2'){
      wx.request({
        url: `https://www.prebeauty.work:3001/api/heart`,
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        data:{
          type:1,
          time:time,
          goodsname:this.data.list.goodsname,
          goodsid:this.data.list.goodsid,
          jiuzhuzhan:`${this.data.region[0]}${this.data.region[1]}${this.data.region[2]}${this.data.addr}`,
          userid:this.data.id
        }
      })
    }
  },

  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      city:true
    })
  }
})